using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster.Business
{
	
	public partial class FormController
	{

		private void GetFieldRuleScripts(Control phForm, HtmlInputHidden disabledVals)
		{
			string[] aryTargetID = null;
			WebControl trgtCtl = default(WebControl);
			string trgtCtlAryStr = string.Empty;
			string trgtLblAryStr = default(string);
			string trgtValAryStr = default(string);
			string strRuleFunction = string.Empty;
			string strRulesHeader = string.Empty;
			string strRuleBody1 = string.Empty;
			string strRuleBody2 = string.Empty;
			string strRulesFooter = string.Empty;
			
			string strPageLoadFunction = string.Empty;
			string strPageLoadBodies = string.Empty;
			string strPageLoadHeader = string.Empty;
			string strPageLoadFooter = string.Empty;
			
			string strTempScript = string.Empty;
			string strRuleVarName = string.Empty;
			string fileText = string.Empty;
			
			if (_fieldRules.Count > 0)
			{
				string filePath = default(string);
				
				//Page Load Header
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/RulesCommon.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						fileText = iFile.ReadToEnd();
					}
					
					if (HttpContext.Current.Request.ApplicationPath == "/")
					{
						fileText = fileText.Replace("[APPPATH]", "");
					}
					else
					{
						fileText = fileText.Replace("[APPPATH]", HttpContext.Current.Request.ApplicationPath);
					}
					fileText = fileText.Replace("[DISABLED__VALIDATORS_ID]", disabledVals.ClientID);
					
					_pageScripts[Constants.FUNCTIONS].Add(fileText);
				}
				else
				{
					return;
				}
				
				_pageScripts[Constants.PAGELOAD].Add(Environment.NewLine + "C5SLLC_RunPageRules();" + Environment.NewLine);
				
				// Field Rules Header
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/RuleHeaderT.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						strRulesHeader = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				
				// Field Rules Body 1
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/RuleBody1.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						strRuleBody1 = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				
				// Field Rules Body 2
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/RuleBody2.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						strRuleBody2 = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				
				// Field Rules Footer
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/RuleFooterT.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						strRulesFooter = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				
			}
			
			List<FieldRulesScriptInfo> sourceFieldRules = default(List<FieldRulesScriptInfo>);
			List<string> addedSourceFields = new List<string>();
			
			foreach (FieldRulesScriptInfo fRule in _fieldRules)
			{
				strRuleFunction = string.Empty;
				
				if (!addedSourceFields.Contains(fRule.SourceField.SourceField))
				{
					strRuleFunction = strRulesHeader.Replace("[FUNCTIONNAME]", "fn" + fRule.SourceField.SourceField);
					strPageLoadBodies += (string) (strRuleBody1.Replace("[SOURCECONTROLID]", fRule.SourceField.SourceField).Replace("[CONTROLTYPE]", (string) (((ControlType) fRule.SourceField.FieldType).ToString())));
					
					
					string ruleToFind = fRule.SourceField.SourceField;
					sourceFieldRules = (from f in _fieldRules where f.SourceField.SourceField == ruleToFind select f).ToList();
					
					foreach (FieldRulesScriptInfo scriptInfo in sourceFieldRules)
					{
						if (!addedSourceFields.Contains(scriptInfo.SourceField.SourceField))
						{
							addedSourceFields.Add(scriptInfo.SourceField.SourceField);
						}
						
						//Create the Control ID for the target Controls
						trgtCtlAryStr = string.Empty;
						aryTargetID = scriptInfo.SourceField.FormFields.Split(',');
						for (int i = 0; i <= aryTargetID.Length - 1; i++)
						{
							trgtCtl = scriptInfo.TargetControls[aryTargetID[i]];
							trgtCtlAryStr += "\'" + trgtCtl.ClientID + "\'";
							if (i < aryTargetID.Length - 1)
							{
								trgtCtlAryStr += ",";
							}
						}
						
						//Create the Control IDs for the Label Array
						trgtLblAryStr = string.Empty;
						for (int i = 0; i <= scriptInfo.TargetLabels.Count - 1; i++)
						{
							trgtLblAryStr += "\'" + scriptInfo.TargetLabels[i].ClientID + "\'";
							if (i < scriptInfo.TargetLabels.Count - 1)
							{
								trgtLblAryStr += ",";
							}
						}
						
						//Create the Control ID's for the Validators Array
						trgtValAryStr = string.Empty;
						for (int i = 0; i <= scriptInfo.TargetValidators.Count - 1; i++)
						{
							trgtValAryStr += "\'" + scriptInfo.TargetValidators[i].ClientID + "\'";
							if (i < scriptInfo.TargetValidators.Count - 1)
							{
								trgtValAryStr += ",";
							}
						}
						
						strRuleVarName = "rul" + sourceFieldRules.IndexOf(scriptInfo) + scriptInfo.SourceField.SourceField;
						strTempScript = strRuleBody2;
						strTempScript = strTempScript.Replace("[RULENAME]", strRuleVarName);
						strTempScript = strTempScript.Replace("[CONTAINS]", scriptInfo.SourceField.ContainsValue);
						strTempScript = strTempScript.Replace("[CONTROLSARRAY]", trgtCtlAryStr);
						strTempScript = strTempScript.Replace("[LABELSARRAY]", trgtLblAryStr);
						strTempScript = strTempScript.Replace("[VALIDATORSARRAY]", trgtValAryStr);
						strTempScript = strTempScript.Replace("[RULEVARNAME]", strRuleVarName);
						
						strRuleFunction += strTempScript + Environment.NewLine;
						
					}
					sourceFieldRules.Clear();
				}
				
				if (!string.IsNullOrEmpty(strRuleFunction))
				{
					strRuleFunction += strRulesFooter.Replace("[RULENAME]", strRuleVarName);
					_pageScripts[Constants.FUNCTIONS].Add(strRuleFunction);
				}
			}
			
			_pageScripts[Constants.PAGELOAD].Add(strPageLoadBodies);
		}
		
		public void GeneratePageScripts(Control phForm, Dictionary<string,List<string>> pageScripts,  bool isEditMode = false, bool ajaxEnabled = false)
		{
			_pageScripts = pageScripts;
			GeneratePageScripts(phForm, isEditMode, ajaxEnabled);

		}
		
		public void GeneratePageScripts(Control phForm,  bool isEditMode = false, bool ajaxEnabled = false)
		{
			
			string fileText = string.Empty;
			string scriptLine = default(string);
			StringBuilder sbCompleteScript = new StringBuilder();
			
			string filePath = default(string);
			
			// Only output the Cascade Declarations if there are cascading dropdowns on the form
			if (_hasCascadingDropdowns)
			{
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/CascadeDeclaration.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						fileText = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				
				fileText = fileText.Replace("[WAITMSG]", Localization.GetString("WaitMsg", Utility.GetSharedResourceFile()));
				fileText = fileText.Replace("[NOTFOUNDMSG]", Localization.GetString("NotFoundMsg", Utility.GetSharedResourceFile()));
				if (HttpContext.Current.Request.ApplicationPath == "/")
				{
					fileText = fileText.Replace("[APPPATH]", "");
				}
				else
				{
					fileText = fileText.Replace("[APPPATH]", HttpContext.Current.Request.ApplicationPath);
				}
				
				_pageScripts[Constants.DECLARATIONS].Add(fileText);
				
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/CascadeFunctions.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						fileText = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				fileText = fileText.Replace("[MODULEID]", this.FormInfo.ModuleID.ToString());
				_pageScripts[Constants.FUNCTIONS].Add(fileText);
			}
			
			// Output any functions requred for Edit Form
			if (isEditMode)
			{
				
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/FormEditDeclaration.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						fileText = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				fileText = fileText.Replace("[PIN]", Localization.GetString("Pin", Utility.GetSharedResourceFile()));
				fileText = fileText.Replace("[UNPIN]", Localization.GetString("UnPin", Utility.GetSharedResourceFile()));
				fileText = fileText.Replace("[ROWMANAGER]", Localization.GetString("RowMgmtHeader", Utility.GetSharedResourceFile()));
				fileText = fileText.Replace("[COLMANAGER]", Localization.GetString("ColMgmtHeader", Utility.GetSharedResourceFile()));
				fileText = fileText.Replace("[MOVECTL]", Localization.GetString("MoveCtlHeader", Utility.GetSharedResourceFile()));
				fileText = fileText.Replace("[COPYCTL]", Localization.GetString("CopyCtlHeader", Utility.GetSharedResourceFile()));
				_pageScripts[Constants.DECLARATIONS].Add(fileText);
				
				if (DotNetNuke.Common.Globals.DataBaseVersion.Major >= 6)
				{
					filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/FormEditReady6.txt");
				}
				else
				{
					filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/FormEditReady.txt");
				}
				
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						fileText = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				_pageScripts[Constants.DOCREADY].Add(fileText);
				
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/FormEditPageLoad.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						fileText = iFile.ReadToEnd();
					}
					
				}
				else
				{
					return;
				}
				_pageScripts[Constants.PAGELOAD].Add(fileText);
			}
			
			// Output the script Declarations
			foreach (string tempLoopVar_scriptLine in _pageScripts[Constants.DECLARATIONS])
			{
				scriptLine = tempLoopVar_scriptLine;
				sbCompleteScript.Append(scriptLine);
			}
			
			// Output any common functions
			if (_pageScripts[Constants.FUNCTIONS].Count() > 0 || _pageScripts[Constants.PAGELOAD].Count() > 0)
			{
				// Common Functions
				filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/CommonFunctions.txt");
				filePath = HttpContext.Current.Server.MapPath(filePath);
				if (File.Exists(filePath))
				{
					using (StreamReader iFile = File.OpenText(filePath))
					{
						_pageScripts[Constants.FUNCTIONS].Add(iFile.ReadToEnd());
					}
					
				}
				else
				{
					return;
				}
			}
			
			// Output the script Functions
			foreach (string tempLoopVar_scriptLine in _pageScripts[Constants.FUNCTIONS])
			{
				scriptLine = tempLoopVar_scriptLine;
				sbCompleteScript.Append(scriptLine);
			}
			
			// jQuery Document Ready Output
			if (_pageScripts[Constants.DOCREADY].Count() > 0)
			{
				
				// Call the noConflict() jQuery method
				//sbCompleteScript.Append(Environment.NewLine & "jQuery.noConflict();" & Environment.NewLine)
				
				// Start jQuery().ready() Section
				sbCompleteScript.Append(Environment.NewLine + "jQuery(document).ready(function () {" + Environment.NewLine);
				
				// Output the dynamic jQuery.ready section lines for each Cascading Control
				foreach (string tempLoopVar_scriptLine in _pageScripts[Constants.DOCREADY])
				{
					scriptLine = tempLoopVar_scriptLine;
					sbCompleteScript.Append(scriptLine);
				}
				
				// End jQuery().ready() Section
				sbCompleteScript.Append("});" + Environment.NewLine);
			}
			
			// Output the Ajax pageLoad code which gets called on Partial Postback
			if (_pageScripts[Constants.PAGELOAD].Count() > 0 && ajaxEnabled)
			{
				
				// Start Ajax pageLoad() Section
				sbCompleteScript.Append(Environment.NewLine + Environment.NewLine + "function pageLoad() {" + Environment.NewLine);

				foreach (string tempLoopVar_scriptLine in _pageScripts[Constants.PAGELOAD])
				{
					scriptLine = tempLoopVar_scriptLine;
					sbCompleteScript.Append(scriptLine);
				}

				foreach (string tempLoopVar_scriptLine in _pageScripts[Constants.PAGELOADEND])
				{
					scriptLine = tempLoopVar_scriptLine;
					sbCompleteScript.Append(scriptLine);
				}
				
				// End Ajax pageLoad() Section
				sbCompleteScript.Append(Environment.NewLine + "}" + Environment.NewLine);
			}
			
			if (!phForm.Page.ClientScript.IsClientScriptBlockRegistered(String.Format("C5SLLC_ScriptBlock_{0}", this._formInfo.FormID)))
			{
				phForm.Page.ClientScript.RegisterClientScriptBlock(phForm.Page.GetType(), String.Format("C5SLLC_ScriptBlock_{0}", this._formInfo.FormID), sbCompleteScript.ToString(), true);
			}
		}
		
		private void GetCascadedControlScript(int page, FormControlInfo ctl)
		{
			
			string ctlScript = default(string);
			FormControlInfo childCtlInfo = default(FormControlInfo);
			bool hasGrandChild = false;
			
			if (string.IsNullOrEmpty(ctl.ParentControlID))
			{
				ctlScript = string.Format(Constants.LOADALLITEMS, ctl.ID, Environment.NewLine);
				_pageScripts[Constants.PAGELOADEND].Add(ctlScript);
			}
			
			// Declaration for this Control
			ctlScript = string.Format(Constants.DECLARATION, ctl.TableRow, ctl.TableCol, page, Environment.NewLine);
			_pageScripts[Constants.DECLARATIONS].Add(ctlScript);
			
			// jQuery Selector for this control
			ctlScript = string.Format(Constants.SELECTOR, ctl.TableRow, ctl.TableCol, page, ctl.ID, Environment.NewLine);
			//_pageScripts(Constants.DOCREADY).Add(ctlScript)
			_pageScripts[Constants.PAGELOAD].Add(ctlScript);
			
			ctlScript = string.Format(Constants.SELF, ctl.TableRow, ctl.TableCol, page, ctl.ID, Environment.NewLine);
			_pageScripts[Constants.PAGELOAD].Add(ctlScript);
			
			if (ctl.IsParentList)
			{
				childCtlInfo = this.GetFormControlInfo(ctl.ChildControlID);
				if (childCtlInfo != null)
				{
					ctlScript = string.Format(Constants.CHILD, ctl.TableRow, ctl.TableCol, page, ctl.ChildControlID, Environment.NewLine);
					//_pageScripts(Constants.DOCREADY).Add(ctlScript)
					_pageScripts[Constants.PAGELOAD].Add(ctlScript);

					if (childCtlInfo.IsParentList)
					{
						ctlScript = string.Format(Constants.GRANDCHILD, ctl.TableRow, ctl.TableCol, page, childCtlInfo.ChildControlID, Environment.NewLine);
						//_pageScripts(Constants.DOCREADY).Add(ctlScript)
						_pageScripts[Constants.PAGELOAD].Add(ctlScript);
						hasGrandChild = true;
					}
				}
			}
			
			if (ctl.IsChildList)
			{
				ctlScript = string.Format(Constants.PARENT, ctl.TableRow, ctl.TableCol, page, ctl.ParentControlID, Environment.NewLine);
				_pageScripts[Constants.PAGELOAD].Add(ctlScript);
			}
			
			if (!string.IsNullOrEmpty(ctl.PromptText))
			{
				ctlScript = string.Format(Constants.PROMPT, ctl.TableRow, ctl.TableCol, page, ctl.PromptText, Environment.NewLine);
				_pageScripts[Constants.PAGELOAD].Add(ctlScript);
				//ctlScript = String.Format(Constants.SETPROMPT, ctl.TableRow, ctl.TableCol, page, Environment.NewLine)
				//_pageScripts(Constants.PAGELOAD).Add(ctlScript)
			}
			
			if (ctl.IsParentList)
			{
				ctlScript = string.Format(Constants.ONCHANGE_LINE_1, ctl.TableRow, ctl.TableCol, page, "{" + Environment.NewLine);
				ctlScript += string.Format(Constants.ONCHANGE_LINE_2, ctl.TableRow, ctl.TableCol, page, Environment.NewLine);
				if (hasGrandChild)
				{
					ctlScript += string.Format(Constants.ONCHANGE_LINE_3, ctl.TableRow, ctl.TableCol, page, Environment.NewLine);
				}
				ctlScript += "});" + Environment.NewLine;
				_pageScripts[Constants.PAGELOAD].Add(ctlScript);
			}
			_hasCascadingDropdowns = true;
		}
		
		private void GetCaptchaControlScript(Control phForm)
		{
			string filePath = default(string);
			
			//Page Load Header
			filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/CaptchaScript.txt");
			filePath = HttpContext.Current.Server.MapPath(filePath);
			if (File.Exists(filePath))
			{
				using (StreamReader iFile = File.OpenText(filePath))
				{
					_pageScripts[Constants.PAGELOAD].Add(iFile.ReadToEnd());
				}
				
			}
			else
			{
				return;
			}
		}
		//private void GetReCaptchaControlScript(Control phForm)
		//{
		//	if(!phForm.Page.ClientScript.IsClientScriptIncludeRegistered(String.Format("ReCaptchaScript_{0}", this._formInfo.FormID)))
		//		phForm.Page.ClientScript.RegisterClientScriptInclude(String.Format("ReCaptchaScript_{0}", this._formInfo.FormID), @"https://www.google.com/recaptcha/api.js");
		//}
		private void GetReCaptchaControlScript(Control phForm)
		{
			if (!phForm.Page.ClientScript.IsClientScriptBlockRegistered(phForm.Page.GetType(), "C5SLLC_ReCaptchaApi"))
			{
				string script = @"<script id=""c5sllc_recaptcha"" src=""https://www.google.com/recaptcha/api.js?onload=c5sllcCaptchaCallBack&render=explicit"" async defer></script>";
				phForm.Page.ClientScript.RegisterClientScriptBlock(phForm.Page.GetType(), "C5SLLC_ReCaptchaApi", script, false);
			}

			if (!phForm.Page.ClientScript.IsClientScriptBlockRegistered(phForm.Page.GetType(), "C5SLLC_ReCaptchaCallBack"))
			{
				string reFunc =
@" 
function c5sllcCaptchaCallBack() { 
	captchaRendered = true;
	$('div.g-recaptcha').each(function () {
		grecaptcha.render(this, {
			'sitekey': '[SITEKEY]'
		});
	});
}
";
				reFunc = reFunc.Replace("[SITEKEY]", this._formSettings.SiteKey);
				phForm.Page.ClientScript.RegisterClientScriptBlock(phForm.Page.GetType(), "C5SLLC_ReCaptchaCallBack", reFunc, true);
			}
		}
		
		private void GetWizardScripts(Control phForm)
		{
			string filePath = default(string);
			string wizardFunctionName = String.Format("C5SLLC_FormWizard_{0}", this.FormInfo.FormID);
			filePath = Path.Combine(Utility.GetRuntimePath(), "assets/js/WizardFunctions.txt");
			filePath = HttpContext.Current.Server.MapPath(filePath);
			if (File.Exists(filePath))
			{
				using (StreamReader iFile = File.OpenText(filePath))
				{
					string strWzScipt = iFile.ReadToEnd();
					strWzScipt = strWzScipt.Replace("[WIZARDSCRIPTFUNCTION]", wizardFunctionName);
					strWzScipt = strWzScipt.Replace("[FORMIDSELECTOR]", String.Format(".C5SLLC_wizard_{0}", this.FormInfo.FormID));
					strWzScipt = strWzScipt.Replace("[CMDSUBMITSELECTOR]", String.Format(".C5S_cmdSubmit_{0}", this.FormInfo.FormID));
					strWzScipt = strWzScipt.Replace("[STARTSTEPSELECTOR]", String.Format(".C5S_wzrdstart_{0}", this.FormInfo.FormID));
					strWzScipt = strWzScipt.Replace("[STEPNAME]", this._formSettings.WizardStepText);
					strWzScipt = strWzScipt.Replace("[STEPCLASS]", this._formSettings.WizardStepClass);
					strWzScipt = strWzScipt.Replace("[TABLEWIDTH]", this._formSettings.TableWidth);
					strWzScipt = strWzScipt.Replace("[BTNTYPE]", this._formSettings.WizardButtonType.ToString());
					strWzScipt = strWzScipt.Replace("[STARTTEXT]", this._formSettings.WizardStartText);
					strWzScipt = strWzScipt.Replace("[NEXTTEXT]", this._formSettings.WizardNextText);
					strWzScipt = strWzScipt.Replace("[PREVTEXT]", this._formSettings.WizardPrevText);
					strWzScipt = strWzScipt.Replace("[VALGROUP]", this._formInfo.ValidationGroup);

					PortalSettings pSettings = new PortalSettings(this._formInfo.PortalID);

					int intVal;
					int.TryParse(this._formSettings.WizardStartNextImage, out intVal);
					DotNetNuke.Services.FileSystem.FileInfo oFileInfo = FileManager.Instance.GetFile(intVal) as DotNetNuke.Services.FileSystem.FileInfo;
					if (oFileInfo != null)
					{
						strWzScipt = strWzScipt.Replace("[STARTIMG]", pSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName);
					}

					int.TryParse(this._formSettings.WizardNextImage, out intVal);
					oFileInfo = FileManager.Instance.GetFile(intVal) as DotNetNuke.Services.FileSystem.FileInfo;
					if (oFileInfo != null)
					{
						strWzScipt = strWzScipt.Replace("[NEXTIMG]", pSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName);
					}

					int.TryParse(this._formSettings.WizardPrevImage, out intVal);
					oFileInfo = FileManager.Instance.GetFile(intVal) as DotNetNuke.Services.FileSystem.FileInfo;
					if (oFileInfo != null)
					{
						strWzScipt = strWzScipt.Replace("[PREVIMG]", pSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName);
					}

					if (this._formSettings.WizardShowSteps)
					{
						switch (_formSettings.WizardStepsAlign)
						{
							case HorizontalAlign.Left:
								strWzScipt = strWzScipt.Replace("[STEPSCLASS]", "C5SLLC_left");
								break;
							case HorizontalAlign.Center:
								strWzScipt = strWzScipt.Replace("[STEPSCLASS]", "C5SLLC_center");
								break;
							case HorizontalAlign.Right:
								strWzScipt = strWzScipt.Replace("[STEPSCLASS]", "C5SLLC_right");
								break;
							default:
								strWzScipt = strWzScipt.Replace("[STEPSCLASS]", "");
								break;
						}
					}
					else
					{
						strWzScipt = strWzScipt.Replace("[STEPSCLASS]", "C5SLLC_hidden");
					}

					if (!phForm.Page.ClientScript.IsClientScriptBlockRegistered(String.Format("C5SLLC_WizardBlock_{0}", this._formInfo.FormID)))
					{
						phForm.Page.ClientScript.RegisterClientScriptBlock(phForm.Page.GetType(), String.Format("C5SLLC_WizardBlock_{0}", this._formInfo.FormID), strWzScipt, true);
					}
				}
			}
			else
			{
				return;
			}
		}

		public void SetWizardCallScripts(Control dynScripts)
		{
			string wizardFunctionName = String.Format("C5SLLC_FormWizard_{0}", this.FormInfo.FormID);
			StringBuilder sbWizardScriptCall = new StringBuilder();
			// Start Ajax pageLoad() Section

			sbWizardScriptCall.Append("<script type=\"text/javascript\">" + Environment.NewLine);

			sbWizardScriptCall.Append("\tSys.Application.add_load(function() {" + Environment.NewLine);

			sbWizardScriptCall.Append(String.Format("\t{0}();{1}", wizardFunctionName, Environment.NewLine));

			// End Ajax pageLoad() Section
			sbWizardScriptCall.Append("});" + Environment.NewLine);

			sbWizardScriptCall.Append("</script>" + Environment.NewLine);

			LiteralControl literal = new LiteralControl();
			literal.Text = sbWizardScriptCall.ToString();

			if (dynScripts != null)
			{
				dynScripts.Controls.Add(literal);
			}
		}

		public void GenerateCalxScrpts(Control phForm, Control dynScripts)
		{
			// Start jQuery().ready() Section
			StringBuilder sbCompleteScript = new StringBuilder();
			//sbCompleteScript.Append(Environment.NewLine + "jQuery(document).ready(function () {" + Environment.NewLine);
			sbCompleteScript.Append("<script type=\"text/javascript\">" + Environment.NewLine);

			sbCompleteScript.Append("\tSys.Application.add_load(function() {" + Environment.NewLine);

			string script = String.Format("\t$('#{0}').calx();{1}", phForm.ClientID, Environment.NewLine);
			sbCompleteScript.Append(script);

			// End Ajax pageLoad() Section
			sbCompleteScript.Append("});" + Environment.NewLine);

			sbCompleteScript.Append("</script>" + Environment.NewLine);

			LiteralControl literal = new LiteralControl();
			literal.Text = sbCompleteScript.ToString();

			if (dynScripts != null)
			{
				dynScripts.Controls.Add(literal);
			}

			//if (!phForm.Page.ClientScript.IsClientScriptBlockRegistered(String.Format("C5SLLC_CalxScriptBlock_{0}", this.FormInfo.FormID)))
			//{
			//	phForm.Page.ClientScript.RegisterClientScriptBlock(phForm.Page.GetType(), String.Format("C5SLLC_CalxScriptBlock_{0}", this.FormInfo.FormID), sbCompleteScript.ToString(), true);
			//}
		}
	}
}
